//
//  RSMainController.swift
//  WeiBoTest
//
//  Created by 任帅 on 2017/2/23.
//  Copyright © 2017年 RENSHUAI. All rights reserved.
//

import UIKit

class RSMainController: UITabBarController {

   
    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
        setupComposeButton()
        
    }
    //使用代码控制设备的方向
    //设置支持的方向之后，当前的控制器及子控制器都会遵守这个方向
    //如果播放视频，通常是通过modal展现的
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    
       return .portrait
    }
    
    
    func composeStatus() {
        
        print("fabu")
    }
    lazy var composeButton :UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")

}

//extension 类似于OC中的分类,在Swift中还可以用来切分代码块
//可以把相近功能的函数放在一个extension中

extension RSMainController{
    
    //添加撰写按钮
    func setupComposeButton()  {
        
        tabBar.addSubview(composeButton)
        
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
    }
    
    //设置所有子控制器
    
    func setupChildControllers(){
     
        let array: [[String: AnyObject]] = [
            ["clsName":"RSHomeViewController" as AnyObject,"title":"首页" as AnyObject,"imagName":"home" as AnyObject,"visitorInfo":["imageName":"","message":"关注一些人，回这里看看有什么惊喜！"] as AnyObject
            ],
            ["clsName":"RSMessageViewController" as AnyObject,"title":"消息" as AnyObject,"imagName":"message_center" as AnyObject,"visitorInfo":["imageName":"visitordiscover_image_message","message":"登陆后，别人发给你的信息都会在这里"] as AnyObject],
            ["clsName":"UIViewController" as AnyObject],["clsName":"RSDiscoverViewController" as AnyObject,"title":"发现" as AnyObject,"imagName":"discover" as AnyObject,"visitorInfo":["imageName":"visitordiscover_image_message","message":"登陆后，最新最热微博尽在掌握"] as AnyObject],
            ["clsName":"RSProfileViewController" as AnyObject,"title":"我" as AnyObject,"imagName":"profile" as AnyObject,"visitorInfo":["imageName":"visitordiscover_image_profile","message":"登陆后，你的微博都会展示给别人"] as AnyObject],
            ]
        var arrM = [UIViewController]()
        for dict in array {
            
            arrM.append(controller(dict: dict))
            
        }
        viewControllers = arrM
        
    }
    //使用字典创建一个子控制器
    private func controller(dict:[String:AnyObject]) -> UIViewController{
    
        //获得字典内容
        guard let clsName = dict["clsName"] as? String,
              let title = dict["title"] as? String,
              let imagName = dict["imagName"] as? String,
            //将clsName转换成cls
              let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? RSBaseViewController.Type,
             let visitorDict = dict["visitorInfo"] as? [String:String]
        else {
            return UIViewController()
        }
        
        //创建视图控制器
        let vc = cls.init()
        vc.title = title
        //设置控制器的访客信息字典
        vc.visitorInfoDic = visitorDict
        vc.tabBarItem.image = UIImage(named:"tabbar_" + imagName)
        vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + imagName + "_selected")? .withRenderingMode(.alwaysOriginal)
        //设置title颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        //文字大小
//        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont .systemFont(ofSize: 13)], for: .normal)
        let nav = RSNavigationController(rootViewController: vc)
        return nav
 
    }
    
}
